Attribute VB_Name = "SharedStartUp"
'====================================================================================
'SharedStartUp - Each app uses this to start, and share a few core variables
'====================================================================================
'NOTE: Please be careful when editing this page. This same module is used both in _
       the game and the level editor
Option Explicit

Public AppPath As String         'Read `GetAppPath` below
Public Const AnimTimer = 30      '32=24FPS, 31=25FPS, 30=33FPS

Public Const TileSize = 64       'Size of the level tiles
Public Const HalfTileSize = 32   '\2

'Frame information
Public Type PM_FRAME
    Step As Long                 'Increases each frame, but reset every second
    Rate As Long                 'Frames per second, updated every second
    Count As Long                'Total number of frames counted
    Time As Long                 'Time taken to render last frame in milliseconds
    StartTime As Long
    Peak As Long                 'Longest time taken for a frame to render
End Type
Public Frame As PM_FRAME

'--- API for `PreventAppInstances` --------------------------------------------------
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal cCmdShow As Long) As Long
Private Const SW_RESTORE = 9
Private Const SW_MINIMIZE = 6

'====================================================================================
'MAIN: Program execution starts here!
'====================================================================================
Sub Main()
    'Prevent the user from opening two copies of the game
    PreventAppInstances AppTitle
    
    'The source code is stored in a sub folder, so that when you try use _
     `App.Path & "\gfx"` it fails. This function gives the right path depending if _
     you're running the compiled EXE, or in the VB IDE. The reason the source is _
     in a subfolder is so that when building the installer, it can easily ignore _
     the folder for the source code
    AppPath = GetAppPath
    
    'Each app has this function in a 'StartUp' module
    StartProgram
End Sub

'====================================================================================
'InIDE: Are we in the IDE or compiled?
'====================================================================================
Public Function InIDE() As Boolean
    On Local Error GoTo LblError
    
    'Do something that will cause an error only in the IDE.
    'Here, we will divide by zero and cause the universe to end
    Debug.Print 1 / 0
    'If there is no error then the debug statement was ignored - we are compiled
    InIDE = False
    Exit Function
    
LblError:
    'If the debug statement errored we must be in the IDE
    InIDE = True
End Function

'====================================================================================
'GetAppPath: Calculate the right AppPath value
'====================================================================================
Public Function GetAppPath() As String
    'The source code is in a sub folder (so as to easily exclude it from the _
     installer script). This function detects if we are running from the IDE and _
     returns a modified `App.Path` so that all functions that load resources in _
     the game need not worry about if they are running from the IDE or compiled _
     (and the differing directory paths that entails)
    
    'Start with retrieving the basic app.path value
    GetAppPath = VB.App.Path
    'If in IDE, go up a folder because we are in the '_Code' folder.
    If InIDE = True Then GetAppPath = Left(GetAppPath, InStrRev(GetAppPath, "\"))
    'Does it end in "\"? If so, remove it
    If Right(GetAppPath, 1) = "\" Then GetAppPath = Left(GetAppPath, Len(GetAppPath) - 1)
End Function

'====================================================================================
'PreventAppInstances: Prevent two copies of the same program from running
'====================================================================================
Public Sub PreventAppInstances(ByVal FormTitle As String)
    'VB6 provides a boolean to state if another copy does exist, but no more details
    If VB.App.PrevInstance = True Then
        Dim Handle As Long
        'Find the other window by its title and Window Class. I used "GetClassName" _
         API call to find out that "ThunderRT6FormDC" is the class name for VB6 _
         windows. NOTE: The class name is ThunderFormDC when running from the IDE
        Handle = FindWindow("ThunderRT6FormDC", FormTitle)
        'If minimized, restore the window
        ShowWindow Handle, SW_RESTORE
        'Focus the keyboard / bring to front the other instance of Pac Mar
        Handle = SetForegroundWindow(Handle)
        'Terminate this instance
        End
    End If
End Sub

'=== END ============================================================================
'This work is licenced under the Creative Commons Attribution 2.5 License
